package com.demo.serviceI;

import com.demo.exception.ServiceException;
import com.demo.mapper.BaseMapper;
import com.demo.service.BaseService;
import com.demo.tool.paging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class BaseServiceI<T,Q> implements BaseService<T,Q> {

     @Autowired
     private BaseMapper<T> baseMapper;

    @Override
    public T findById(Serializable id) {
        T result=null;
        if(id!=null){
            result= (T) baseMapper.findById(id);
        }

        return result;
    }

    @Override
    public T findByName(String name) {
        T result=null;
        if(StringUtils.hasLength(name)||!name.isEmpty()){
            result= (T) baseMapper.findByName(name);
        }

        return result;
    }

    @Override
    public int save(T t) {
        int result=0;
        if(t!=null){
            result= baseMapper.save(t);
        }
        return result;
    }

    @Override
    public int saveAll(Collection<T> collection) {
       return baseMapper.saveAll(collection);
    }

    @Override
    public int update(T t) {
        int result=0;
        if(t!=null){
            result= baseMapper.update(t);
        }
        return result;
    }

    @Override
    public int delById(Serializable id) {
        int result=0;
        if(id!=null){
            result= baseMapper.delById(id);
        }
        return result;
    }

    @Override
    public int delByIdIn(Serializable[] ids) {
        if(ids.length<=0){
            throw new ServiceException("请输入要删除的记录id");
        }
        return baseMapper.delByIdIn(ids);
    }

    @Override
    public paging findAllpaging(Integer page, Integer pageSize, Q q) throws ParseException {
        if(page==null){
            throw new ServiceException("请输入页码");
        } else if (pageSize==null) {
            throw new ServiceException("请输入页数");
        } else if (ObjectUtils.isEmpty(q)) {
            throw new ServiceException("查询对象为空");
        }
        int countNumber= baseMapper.countNumber(q);
        System.out.println(countNumber+"查出来的");
      paging paging=new paging(page,pageSize,countNumber);
      paging.setData(baseMapper.findAllpaging(paging.getOffset(),paging.getPageSize(),q));
        return paging;
    }

    @Override
    public List<T> findAll(Q q) {
        return baseMapper.findAll(q);
    }





}
